// pages/user/user-upload-idcard/index.js
const app = getApp()
import {
  request,
  uploadImage
} from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    graduation_image: null,
    setIntervalId: null,
    time:3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const setIntervalId = setInterval(() => {
      this.setData({
        time: this.data.time - 1
      })
      if (this.data.time <= 0) {
        clearInterval(setIntervalId)
      }
    }, 1000)
    this.setData({
      setIntervalId
    })
    const {
      graduation_image
    } = app.globalData.userInfo
    this.setData({
      graduation_image
    })
    
  },
  uploadImg() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success:res => {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePath = res.tempFilePaths[0]
        console.log(res)
        uploadImage(tempFilePath).then(res => {
          const uploadRes = JSON.parse(res.data)
          console.log(uploadRes,'uploadRes')
          if(uploadRes.code === 200) {
            tempFilePath = uploadRes.data.url
          }
          this.setData({
            graduation_image:tempFilePath
          })
        })
      }
    })
  },
  goNext() {
    if(this.data.graduation_image){
      request('/api/user/info', app.globalData.token, 'post', {graduation_image:this.data.graduation_image}).then(res => {
        console.log(res, '提交成功~')
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功~',
            duration: 2000,
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 2500)
        } else {
          wx.showToast({
            title: '提交成功~',
            duration: 2000,
            icon: 'none'
          })
        }

      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
clearInterval(this.data.setIntervalId)
this.setData({
  setIntervalId: null
})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})